package com.jpaAutoProcess.selectKernel;

import com.jpaAutoProcess.AutoSelect;
import com.jpaAutoProcess.kernel.AutoKernel;
import com.jpaAutoProcess.kernel.QueryKernel;
import com.jpaAutoProcess.utils.ParamConditionBO;
import com.jpaAutoProcess.utils.SqlConvertEntityUtil;
import com.jpaAutoProcess.utils.parseParamCondition.RequirParametersAndConditions;
import lombok.NonNull;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cc
 * @date 2022/8/22 20:36
 * @Description : 查询
 */
public class NativeQueryJpa<T> {
  private Class<T> targetClass;

  public NativeQueryJpa(Class<T> targetClass) {
    this.targetClass = targetClass;
  }

 /**
   * 获取查询结果集合
   *
   * @param select        select查询语句
   * @param from          from查询语句
   * @param where         where查询语句
   * @param params         where 条件
   * @param entityManager
   * @return List<T>
   * @author C.c
   * @since 2022-08-23 10:39
   **/
  public List<T> getResultList(@NonNull StringBuilder select, @NonNull StringBuilder from, StringBuilder where, Map<String, Object> params, @NonNull EntityManager entityManager) {
    QueryKernel<T> queryKernel = new QueryKernel<T>();
    return queryKernel.getTs(select, from, where, params, entityManager,targetClass);
  }



  public List<T> autoResult(@NonNull StringBuilder select, @NonNull StringBuilder from, RequirParametersAndConditions<ParamConditionBO, Object> databaseParameters, @NonNull EntityManager entityManager){

    AutoKernel autoKernel = new AutoKernel();
    QueryKernel<T> queryKernel = new QueryKernel<>();
    StringBuilder where = new StringBuilder();
    Map<String, Object> params = new HashMap<String, Object>();

    autoKernel.spliceSentencesAndPutParams(databaseParameters, where, params);
    return queryKernel.getTs(select, from, where, params, entityManager, targetClass);
  }

}
